package com.hyt.it.ogt.kq.service.gov.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.service.bm.api.gov.KwGovApi;
import com.hyt.it.ogt.kq.service.model.entity.Subject;
import com.hyt.it.ogt.kq.service.gov.service.ISubjectService;
import com.hyt.it.ogt.kq.service.model.param.ApiFail;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 处理别的服务调用失败的任务
 * 
 * @author liuq
 * @since 2021-08-02
 */
@Component
@Slf4j
public class ApiFailJob {
	
	@Autowired
	KwGovApi bmClient;
	
	@Autowired
    private ISubjectService iSubjectService;
	
	/**
	 * 编辑科目信息
	 */
	@XxlJob("editSubjectJob")
    public void editSubjectJob() throws Exception {
		log.info("执行编辑科目信息任务");
		ApiResponse<List<com.hyt.it.ogt.kq.service.model.param.ApiFail>> apiResponse = bmClient.apiFailList("/api/bm/subject/edit");
		if (apiResponse != null && apiResponse.getData() != null && !apiResponse.getData().isEmpty()) {
			List<ApiFail> apiFailList = apiResponse.getData();
			JSONArray idArray = new JSONArray();
			for (ApiFail apiFail : apiFailList) {
				Subject subject = JSONObject.parseObject(apiFail.getParam(), Subject.class);
		    	subject.setId("1000");
		    	iSubjectService.updateById(subject);
		    	idArray.add(apiFail.getId());
			}
			bmClient.apiFailDelete(idArray.toJSONString());
			XxlJobHelper.log("执行编辑科目信息成功");
		}
    }

}
